package atguigu.com.login.controller;

import atguigu.com.login.VO.ResultVO;
import atguigu.com.login.VO.LoginForm;
import atguigu.com.login.VO.RouterVo;
import atguigu.com.login.pojo.SysUser;
import atguigu.com.login.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Action;
import java.util.List;

@RestController
@RequestMapping("/eduservice/user")
@CrossOrigin
public class LoginContrllor {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping("/login")
    public ResultVO teacherlogin(@RequestBody LoginForm loginForm){
        String token=sysUserService.login(loginForm);
        return ResultVO.ok().Msg("登录成功").Data("token",token);
    }
    @GetMapping("/info")
    public ResultVO getSysUserInfo(String token){
        //String token = request.getHeader("token");
        SysUser sysUser= sysUserService.getSysUserInfoByToken(token);
        List<RouterVo> routers = sysUserService.findMenusByUserId(sysUser.getId());
        List<String> buttons = sysUserService.findButtonByUserId(sysUser.getId());
        return ResultVO.ok().Data("name",sysUser.getName())
                .Data("avatar","")
                .Data("routers",routers).Data("buttons",buttons);
    }
    @GetMapping("/infos/{id}")
    public ResultVO getSysUserInfo(@PathVariable("id") Long userId){
        List<RouterVo> routerVos = sysUserService.findMenusByUserId(userId);
        return ResultVO.ok().Data("routerVos",routerVos);
    }
}
